<?php


namespace App\Http\Controllers\Workflow;


use App\Enums\BaseErrorCode;
use App\Http\Controllers\BaseController;
use App\Models\Workflow\WorkflowV2OrderModel;
use App\Service\Cruise\Mission\WorkflowV2InputService;
use App\Service\Workflow\Web\WorkflowV2OrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class WorkflowV2OrderController extends BaseController
{
    public function list(Request $request)
    {
        $pageSize             = (int)$request->get('pageSize', 10);
        $pageNum              = (int)$request->get('pageNum', 1);
        $shopIds              = $request->get('shopIds');
        $shopIds              = $shopIds ? explode(',', $shopIds) : [];
        $dimCode              = $request->get('dimCode');
        $workflowNo           = $request->get('workflowNo');
        $status               = (string)$request->get('status', '');
        $status               = strlen($status) ? (int)$status : null;
        $currentHandlerUserid = (string)$request->get('currentHandlerUserId');
        $currentHandlerUserid = strlen($currentHandlerUserid) ? (int)$currentHandlerUserid : null;
        $typeId               = (string)$request->get('typeId');
        $typeId               = strlen($typeId) ? (int)$typeId : null;
        $createdAtStart       = $request->get('createdAtStart');
        $createdAtStart       = $createdAtStart ? Carbon::parse($createdAtStart)->startOfDay()->timestamp : null;
        $createdAtEnd         = $request->get('createdAtEnd');
        $createdAtEnd         = $createdAtEnd ? Carbon::parse($createdAtEnd)->endOfDay()->timestamp : null;
        $updatedAtStart       = $request->get('updatedAtStart');
        $updatedAtStart       = $createdAtStart ? Carbon::parse($updatedAtStart)->startOfDay()->timestamp : null;
        $updatedAtEnd         = $request->get('updatedAtEnd');
        $updatedAtEnd         = $updatedAtEnd ? Carbon::parse($updatedAtEnd)->endOfDay()->timestamp : null;

        $data = WorkflowV2OrderService::list(
            $pageSize, $pageNum,
            $shopIds, $dimCode, $workflowNo, $status, $currentHandlerUserid, $typeId, $createdAtStart, $createdAtEnd,
            $updatedAtStart, $updatedAtEnd
        );
        return $this->success($data);
    }

    //详情
    public function detail(Request $request): JsonResponse
    {
        $id = (int)$request->get('id');

        return $this->success(WorkflowV2OrderService::detail($id, getRmsUserInfoByAuth()->id));
    }

    //审批
    public function submitOrder(Request $request): JsonResponse
    {
        $this->validate($request, [
            'params.*.field' => 'required|string',
            'params.*.value' => 'required',
        ]);

        $params = $request->post('params');

        $id            = (int)$request->get('id');
        $cruiseOrderId = (int)$request->get('cruiseOrderId');

        $order = WorkflowV2OrderModel::query()
                                          ->with('currentNodeRel')
                                          ->where('id', $id)
                                          ->first();

        if ($order->currentNodeRel->cruiseOrderId !== $cruiseOrderId) {
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, '当前工单表单ID异常');
        }

        $input = (new WorkflowV2InputService($cruiseOrderId, getRmsUserInfoByAuth()->id));
        $input->setWorkflowOrderId($id);
        foreach ($params as $item){
            $input->saveUserContentJson($item['field'], $item['value'],$item['auditValue'] ?? []);
        }
        $input->submitOrder();

        return $this->success();
    }

    public function closeOrder(Request $request): JsonResponse
    {
        try {
            $id = (int)$request->get('id');
            WorkflowV2OrderService::closeOrder($id,getRmsUserInfoByAuth()->id);
            return $this->success();
        } catch (\Exception $exception){
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL,$exception->getMessage());
        }
    }

}